package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealService;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        //1 获取登陆人
        Long id = (Long) session.getAttribute("employee");
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);

        setmealService.save(setmealDto);
        return R.success("添加套餐成功");
    }

    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page<SetmealDto>> findByPage(Integer page,Integer pageSize,String name){
        Page<SetmealDto> pageObj = setmealService.findByPage(page, pageSize, name);
        return R.success(pageObj);
    }

    @DeleteMapping
    //@RequestParam 加上这个注解springmvc 就把前端传递过来的数据封装成一个list，如果不加默认封装成数组
    @ApiOperation(value = "套餐删除接口")
    public R<String> deleteByIds(@RequestParam List<Long> ids, HttpSession session) {
        //获取操作人
        Long id = (Long) session.getAttribute("employee");
        //调用业务方法去删除
        setmealService.deleteByIds(ids, id);
        //返回数据
        return R.success("删除套餐成功");
    }

    /**
     * 根据套餐的类别展示套餐
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "套餐条件查询接口")
    public R<List<Setmeal>> list(Long categoryId, Integer status) {
        List<Setmeal> setmealList =   setmealService.list(categoryId,status);
        return R.success(setmealList);
    }

    @GetMapping("/{id}")
    public R<SetmealDto> findById(@PathVariable Long id) {
       return R.success(setmealService.findById(id));
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto,HttpSession session) {
        Long id = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(id);
        return setmealService.update(setmealDto);
    }

}